package com.yunchuang.service.impl;

import cn.hutool.core.util.StrUtil;
import com.yunchuang.common.BaseResult;
import com.yunchuang.common.ResultUtil;
import com.yunchuang.dao.UserDao;
import com.yunchuang.dao.impl.UserDaoImpl;
import com.yunchuang.model.dto.UserUpdateDto;
import com.yunchuang.model.dto.UserUploadAvatarDto;
import com.yunchuang.model.pojo.User;
import com.yunchuang.model.dto.UserRegisterDto;
import com.yunchuang.service.UserService;
import com.yunchuang.utils.MD5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 大师的本
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2023-10-21 09:16:10
 */
public class UserServiceImpl implements UserService {
    public static final Logger logger= LoggerFactory.getLogger("logback");
    private final UserDao userDao = new UserDaoImpl();

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 用户密码
     * @param req
     * @return
     */
    @Override
    public BaseResult userLogin(String username, String password, HttpServletRequest req) {
        // 校验
        if (StrUtil.hasEmpty(username, password)) {
            return ResultUtil.error("用户名或密码为空！请重新登录");
        }
        // 密码加密
        String userPassword = MD5Utils.encrypt(password);
        // 查询数据库
        User user = userDao.getByNameAndPassword(username, userPassword);
        if (user == null) {
            return ResultUtil.error("用户未注册！登录失败");
        }
        // 设置 session
        HttpSession session = req.getSession();
        session.setAttribute("user", user);
        session.setMaxInactiveInterval(60*60*24);
        return ResultUtil.success("登录成功！", user);
    }

    /**
     * 用户注册
     *
     * @param userRegisterDto 注册 User 类
     * @return
     */
    @Override
    public boolean userRegister(UserRegisterDto userRegisterDto) {
        // 解析 UserRegisterDto
        String username = userRegisterDto.getUserName();
        String password = userRegisterDto.getUserPassword();
        String email = userRegisterDto.getEmail();
        // 校验
        if (StrUtil.hasEmpty(username, password, email)) {
            logger.info("注册字段为空！注册失败！");
            return false;
        }
        User user = userDao.selectByUserId(username);
        if (user != null) {
            logger.info("用户已注册！注册失败！");
            return false;
        }
        // 密码加密
        String userPassword = MD5Utils.encrypt(password);
        // 存储数据至数据库
        int flag = userDao.insertUser(username, userPassword, email);
        if (flag == -1) {
            logger.info("注册失败！");
            return false;
        }
        logger.info("注册成功！");
        return true;
    }

    /**
     * 修改用户信息
     * @param userUpdateDto
     * @return
     */
    @Override
    public boolean userUpdate(UserUpdateDto userUpdateDto) {
        if (StrUtil.isEmptyIfStr(userUpdateDto)) {
            logger.info("用户修改字段为空");
            return false;
        }
        // 密码加密存储
        String password = userUpdateDto.getPassword();
        String encryptPassword = MD5Utils.encrypt(password);
        userUpdateDto.setPassword(encryptPassword);

        int flag = userDao.updateUser(userUpdateDto);
        if (flag == -1) {
            logger.info("修改用户信息失败！");
            return false;
        }
        logger.info("修改用户信息成功");
        return true;
    }

    @Override
    public BaseResult userById(int userId) {
        User user = userDao.selectByUserId(userId);
        if (user == null) {
            return ResultUtil.error("查询为空！");
        }
        return ResultUtil.success(user);
    }

    @Override
    public BaseResult userUpdateAvatar(UserUploadAvatarDto userUploadAvatarDto) {
        if (StrUtil.isEmptyIfStr(userUploadAvatarDto)) {
            return ResultUtil.error("修改头像信息为空！");
        }
        int flag = userDao.updateUserAvatar(userUploadAvatarDto);
        if (flag == -1) {
            return ResultUtil.error("修改头像信息失败！");
        }
        return ResultUtil.success("修改头像信息成功！");
    }
}




